package com.wenwu.controller.common;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wenwu.common.bean.RelicsAttachment;
import com.wenwu.common.bean.RelicsOrganization;
import com.wenwu.common.exception.RelicsException;
import com.wenwu.common.page.PageUtils;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.common.utils.JsonUtils;
import com.wenwu.common.utils.Status.AttachmentFrom;
import com.wenwu.service.OrganizationService;

/**
 * 机构信息管理
* @ClassName: OrganizationController 
* @Description: TODO 
* @author 肖崇高  xiaochonggao@zkingsoft.com 
* @date 2015年5月15日 下午4:23:29 
*
 */
@Controller(value = "OrganizationController")
@RequestMapping(value = "/mgr/organ")
public class OrganizationController {
	
	Logger log = Logger.getLogger(this.getClass());
	
	// 用来创建json数据
	private JsonGenerator jsonGenerator = null;

	@Autowired
	OrganizationService organizationService;
	// 进入页面
	@RequestMapping(value = "/init")
	public String initUserPage() {
		log.info("进入机构管理页面...");
		return "organ/organization-index";
	}
	// 查询机构数据
	@RequestMapping(value = "/showList")
	public @ResponseBody PaginationVO showList(Model model,
			HttpServletRequest request, RelicsOrganization organization)
			throws Exception {
		log.info("查询数据...");
		PaginationVO pageVo = PageUtils.getPaginationInfo(request);
		try {
			int totalRecord = organizationService.selectTotalRecord(organization);// 查询符合条件的个数
			List<RelicsOrganization> list = organizationService.selectList(
					organization, pageVo);// 查询符合条件的数据
			pageVo.setList(list);// 将数据设置到PaginationVO对象中
			pageVo.setTotalRecord(totalRecord);
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage());
			model.addAttribute("errorMsg", e.getMessage());
		}
		return pageVo;
	}
	// 添加数据
	@RequestMapping(value = "/addObj",method=RequestMethod.POST)
	public void addObj(Model model,RelicsOrganization organization,
			HttpServletResponse response,HttpServletRequest request) throws Exception {
		jsonGenerator = JsonUtils.getJsonGenerator(response);
		log.info("添加数据..."+organization);
		log.info("名称："+request.getAttribute("organName")+"    organName = "+request.getParameter("organName"));
		try {
			jsonGenerator.writeObject(organizationService.addObj(organization));
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage(),e);
			model.addAttribute("errorMsg", e.getMessage());
		}
	}

	/**
	 * 显示要修改的信息
	 * 
	 * @param response
	 * @param userId
	 * @throws Exception
	 */
	@RequestMapping(value = "/showUpdate")
	public void showUpdate(HttpServletResponse response,
			@RequestParam(value = "organId") Integer organId)
			throws Exception {
		if (organId != null) {
			RelicsOrganization dictionary = organizationService
					.selectByKey(organId);
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			jsonGenerator.writeObject(dictionary);
		}
	}

	/**
	 * 删除信息
	 * @param response
	 * @param request
	 * @param key
	 * @throws Exception
	 */
	@RequestMapping(value = "/deleteInfo")
	public void deleteInfo(HttpServletResponse response,
			HttpServletRequest request,
			@RequestParam(required = false, value = "key") String key)
			throws Exception {
		log.info("主键：" + key);
		try {
			String result = organizationService.deleteBykeys(key);
			jsonGenerator.writeObject("成功删除" + result + "条数据");
		} catch (Exception e) {
			jsonGenerator.writeObject("删除数据失败");
		}
	}
		
	// 进入页面
	@RequestMapping(value = "/form")
	public String turnToForm() {
		log.info("进入机构管理页面...");
		return "organ/organization-form";
	}
		

}
